// index page
// ============

define(
    [ 'require', 'zepto', 'wx', 'ejs', 'utils', 'sui' ],
    function(require, $, wx, ejs, utils) {
      return {
        init : function(data) {
          this.initPage()
          this.unionid = data.unionid  // "o4UI81Pq-DgJTK_mawcG2IGnWIkk"
          utils.configWeixin(data.jsConfig, true, {
            name: "2018健身精英训练营，11月17日，燃爆全场。快来参加吧~",
            desc: "您的小伙伴邀请您参加2018健身精英训练营，11月17日，燃爆全场。快来参加吧~",
            link: window.location.href,
            cover : window.__CTX__.domain + "/resource/images/util/index-cover.png"
          });
        },
        initPage : function() {
          var that = this;
          var html = '<div class="ua-title"><div class="ua-title"><p><span class="iconfont icon-menu"></span>&nbsp;&nbsp;课程介绍</p></div></div>';
          html = html
              + '<div class="course-all">'
              + '<div class="course-title"><%= productAll.name %></div>'
              + '<div class="clearfix">'
              + '<div class="pull-left" style="padding: .3rem 0">'
              + '<%- productAll.description %>'
              + '</div>'
              + '<div class="pull-right">'
              + '<div class="eq-area">'
              + '<p class="sub-title">精英装备包</p>'
              + '<p><%= productAll.equipment_package %></p>'
              + '<p style="text-align: right;"><%= productAll.equipment_package_price%></p>'
              + '</div>'
              + '<div class="price-area">'
              + '<p><strong class="price-strong">价格</strong>：<span style="color: #DA1F36;">￥<%= productAll.price / 100 %>(名额:<%= remaing[productAll.id].rests %>/<%= remaing[productAll.id].totals %>人)</span></p>'
              + '</div></div></div>'
              + '<% if (userCoursesMapSize > 0 && userCoursesMap[productAll.id] == null) { %>'
              + '<div style="width: 7rem; margin: 0 auto;">'
              + '<p style="margin: .3rem 0 .75rem 0;"><a href="#" class="button button-fill button-buy disabled">您已购买其他课程</a></p>'
              + '</div>'
              + '<% } else if (userCoursesMapSize > 0 && userCoursesMap[productAll.id] != null) {  %>'
              + '<div style="width: 5rem; margin: 0 auto;">'
              + '<p style="margin: .3rem 0 .75rem 0;"><a href="#" class="button button-fill button-buy disabled">您已购买</a></p>'
              + '</div>'
              + '<% } else if (remaing[productAll.id].rests == 0) { %>'
              + '<div style="width: 5rem; margin: 0 auto;">'
              + '<p style="margin: .3rem 0 .75rem 0;"><a href="#" class="button button-fill button-buy disabled">报名已满</a></p>'
              + '</div>'
              + '<% } else if (userCoursesMapSize == 0) { %>'
              + '<div style="width: 5rem; margin: 0 auto;">'
              + '<p style="margin: .3rem 0 .75rem 0;"><a href=' + window.__CTX__.root + '/shopping?sku=<%=productAll.id%> class="button button-fill button-buy">立即购买</a></p>'
              + '</div>'
              + '<% } %>'
              + '</div>'
              + '<% products.forEach(function(item, index){%>'
              + '<% if (index > 0) { %>'
              + '<div class="course-item">'
              + '<div class="course-item-title"><%= item.name %></div>'
              + '<div class="clearfix">'
              + '<div class="<%= index % 2 == 1 ? "pull-left" : "pull-right" %>" style="width: 40%;">'
              + '<% if(item.single_avatar == false) { %>'
              + '<div class="logo">'
              + '<img src="'
              + window.__CTX__.resource
              + '<%= item.coach_avatar %>.png?v=1.1.1" alt="" />'
              + '</div>'
              + '<% } else { %>'
              + '<% item.coach_avatar_list.forEach(function(imageItem){%>'
              + '<div class="logo">'
              + '<img src="'
              + window.__CTX__.resource
              + '<%= imageItem %>.png?v=1.1.1" alt="" />'
              + '</div>'
              + '<% }) %>'
              + '<% } %>'
              + '<% if (userCoursesMapSize > 0 && userCoursesMap[productAll.id] != null) {  %>'
              + '<div style="width: 7rem; margin: 0 auto;">'
              + '<p style="margin: 1.5rem 0 .75rem 0;"><a href="#" class="button button-fill button-buy disabled">您已购买全套课程</a></p>'
              + '</div>'
              + '<% } else if (userCoursesMapSize > 0 && userCoursesMap[item.id] != null) { %>'
              + '<div style="width: 5rem; margin: 0 auto;">'
              + '<p style="margin: 1.5rem 0 .75rem 0;"><a href="#" class="button button-fill button-buy disabled">您已购买</a></p>'
              + '</div>'
              + '<% } else if (remaing[item.id].rests == 0) { %>'
              + '<div style="width: 5rem; margin: 0 auto;">'
              + '<p style="margin: 1.5rem 0 .75rem 0;"><a href="#" class="button button-fill button-buy disabled">报名已满</a></p>'
              + '</div>'
              + '<% } else { %>'
              + '<div style="width: 5rem; margin: 0 auto;">'
              + '<p style="margin: 1.5rem 0 .75rem 0;"><a href='+window.__CTX__.root +'/shopping?sku=<%=item.id%> class="button button-fill button-buy">立即购买</a></p>'
              + '</div>'
              + '<% } %>'
              + '</div>'
              + '<div class="<%= index % 2 == 1 ? "pull-right" : "pull-left" %>" style="width: 60%;">'
              + '<div class="description">'
              + '<p style="margin: 0px;"><%= item.description %></p>'
              + '<p style="margin: 0px;">课程时间：<%= item.interval_time %></p>'
              + '</div>'
              + '<div class="eq-area">'
              + '<p class="sub-title"><strong>讲师介绍</strong></p>'
              + '<% if(item.single_avatar == false) { %>'
              + '<p style="margin: 0px;"><strong class="price-strong"><%=item.coach_name%></strong>: <%= item.coach_description %></p>'
              + '<% } else { %>'
              + '<% item.coach_description_list.forEach(function(description, index){%>'
              + '<p style="margin: 0px;"><strong class="price-strong"><%=item.coach_name_list[index]%></strong>: <%= description %></p>'
              + '<% }) %>'
              + '<% } %>'
              + '</div>'
              + '<div class="eq-area">'
              + '<p class="sub-title"><strong>装备包</strong></p>'
              + '<p style="margin: 0px;"><%= item.equipment_package %></p>'
              + '<p style="margin: 0px; text-align: right; padding-right: 1.2rem;"><%= item.equipment_package_price %></p>'
              + '</div>'
              + '<div class="price-area">'
              + '<p style="margin: 0px;"><strong class="price-strong">价格</strong>:&nbsp;<span style="color: #DA1F36;">￥<%= item.price / 100 %>&nbsp;(名额:<%= remaing[item.id].rests%>/<%= remaing[item.id].totals %>人)</span></p>'
              + '</div>' + '</div>' + '</div>' + '</div>'
              + '<% } %>' + '<% }) %>';
          $.ajax({
            type : 'GET',
            url : window.__CTX__.domain + '/api/products',
            dataType : 'json',
            timeout : 30000,
            success : function(res) {

              if (res.code == 0) {
                that.products = res.data.products;
                $.ajax({
                  type : 'GET',
                  url : window.__CTX__.domain
                      + '/api/products/remaining',
                  dataType : 'json',
                  timeout : 30000,
                  success : function(res) {
                    if(res.code == 0) {
                      that.remaing = res.data.remaining;
                      $.ajax({
                        type : 'GET',
                        url : window.__CTX__.domain + '/api/profile/courses?unionid=' + that.unionid,
                        dataType : 'json',
                        timeout : 30000,
                        success : function(res) {
                          console.log(res);
                          if(res.code == 0) {
                            that.userCourses = res.data.courses;
                            that.userCoursesMap = res.data.coursesMap;
                            that.userCoursesMapSize = Object.keys(res.data.coursesMap).length;
                            var result = ejs.render(html, {
                              productAll : that.products[0],
                              products : that.products,
                              remaing : that.remaing,
                              userCoursesMap: that.userCoursesMap,
                              userCoursesMapSize: that.userCoursesMapSize
                            })
                            $('.course-list').html(result);
                          } else {
                            $.toast("获取课程数据失败");
                          }
                        },
                        error : function(xhr, type) {
                          alert('Ajax error!')
                        }
                      })
                    } else {
                      $.toast("获取课程数据失败");
                    }

                  },
                  error : function(xhr, type) {
                    alert('Ajax error!')
                  }
                })
              } else {
                $.toast("获取课程数据失败");
              }
            },
            error : function(xhr, type) {
              alert('Ajax error!')
            }
          })
        }
      }
    })